<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="CHT模号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chtMoldNum">
              <j-search-select-tag placeholder="请输入CHT模号" v-model="model.chtMoldNum" :dictOptions="dictOptions"></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="试模次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldTestNum">
              <j-search-select-tag
                v-model="model.moldTestNum"
                dict="yl_mold_test_num"
                placeholder="请输入试模次数"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="项目人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectPersonnel">
              <j-search-select-tag
                v-model="model.projectPersonnel"
                dict="yl_projectPersonnel"
                placeholder="请输入项目人员"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="试模人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldTester">
              <j-search-select-tag
                v-model="model.moldTester"
                dict="yl_moldTester"
                placeholder="请输入试模人员"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="试模机号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trimmerNo">
              <a-input v-model="model.trimmerNo" placeholder="请输入试模机号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="试模场地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldTestSite">
              <j-search-select-tag
                v-model="model.moldTestSite"
                dict="yl_moldTestSite"
                placeholder="请输入试模场地"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="用时(H)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timeUsed">
              <a-input v-model="model.timeUsed" placeholder="请输入用时(H)" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="用料(KG)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialUsed">
              <a-input v-model="model.materialUsed" placeholder="请输入用料(KG)" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="质量目标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qualityObjectives">
              <a-input v-model="model.qualityObjectives" placeholder="请输入质量目标" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="要求试模时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="requestMoldTestTime">
              <j-date placeholder="请选择要求试模时间" v-model="model.requestMoldTestTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="实际试模时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actualMoldTestTime">
              <j-date placeholder="请选择实际试模时间" v-model="model.actualMoldTestTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="医疗试模子表" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="ylMoldTestChildTable.loading"
          :columns="ylMoldTestChildTable.columns"
          :dataSource="ylMoldTestChildTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MoldTrialQualityTableForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
        },
        dictOptions:[],
        testres:[],
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['ylMoldTestChild', ],
        tableKeys:['ylMoldTestChild', ],
        activeKey: 'ylMoldTestChild',
        // 医疗试模子表
        ylMoldTestChildTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '试模主表id',
              key: 'moldTrialQualityId',
              type: FormTypes.hidden,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '试模内容',
              key: 'moldTestContent',
              type: FormTypes.select,
              dictCode: 'yl_moldTestContent',
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '试模状态',
              key: 'moldTestStatus',
              type: FormTypes.select,
              dictCode: 'yl_moldTestStatus',
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '提交物',
              key: 'submissions',
              type: FormTypes.file,
              token:true,
              responseName:"message",
              width:"200px",
              placeholder: '请选择文件',
              defaultValue:'',
            },
            {
              title: '版本',
              key: 'version',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/ch/moldTrialQualityTable/add",
          edit: "/ch/moldTrialQualityTable/edit",
          queryById: "/ch/moldTrialQualityTable/queryById",
          queryAllMoldNum: "ch/ylProjectInfo/queryAllMoldNum",
          ylMoldTestChild: {
            list: '/ch/moldTrialQualityTable/queryYlMoldTestChildByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.queryAllMoldNum();
    },
    methods: {
      queryAllMoldNum(){
        getAction(this.url.queryAllMoldNum).then((res)=>{
              if(res.success){
                this.testres = res.result;
                for(let i = 0;i<this.testres.length;i++){
                  this.dictOptions.push({text:this.testres[i],value:this.testres[i]})
                }
                console.log(this.testres);
              }
            })
      },
      addBefore(){
        this.ylMoldTestChildTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.ylMoldTestChild.list, params, this.ylMoldTestChildTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          ylMoldTestChildList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>